package com.geoserver.service;

import com.alibaba.fastjson.JSON;
import com.geoserver.dao.TreeInfoDao;
import com.geoserver.entity.TreeInfo;
import com.geoserver.entity.TreeName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * FileName: TreeInfoService
 * Create by fengzheng on 2019/4/20 2:43
 * Description: 树木信息
 */

@Service
public class TreeInfoService {

    private TreeInfoDao treeInfoDao;

    @Autowired
    public TreeInfoService(TreeInfoDao treeInfoDao) {
        this.treeInfoDao = treeInfoDao;
    }

    /**
     * 查找树木信息
     */
    public String findTreeInfo(int ID){
        // 存放结果集
        HashMap<String,Object> resultMap = new HashMap<>();
        // 存放查找的数据信息
        ArrayList<HashMap> infoArrayList = new ArrayList<>();

        try{
            int treeNumber = treeInfoDao.countTreeNum();
            HashMap<String,Integer> treeNumMap = new HashMap<>();
            treeNumMap.put("treeNumber",treeNumber);
            treeNumMap.put("currentNum",ID+5);
            resultMap.put("treeNum",treeNumMap);

            ArrayList<TreeName> treeNameList = treeInfoDao.findTreeName(ID);
            if(treeNameList != null){
                resultMap.put("status","ok");
                for(TreeName treeName: treeNameList){
                    HashMap<String,ArrayList> treeMap = new HashMap<>();
                    ArrayList<TreeInfo> treeInfoList = treeInfoDao
                            .findTreeInfo(treeName.getTreeName());
                    treeMap.put(treeName.getTreeName(),treeInfoList);
                    infoArrayList.add(treeMap);
                }
                resultMap.put("info",infoArrayList);
            }
        }catch (Exception e1){
            e1.printStackTrace();

            resultMap.put("status","error");
            return JSON.toJSONString(resultMap);
        }
        return JSON.toJSONString(resultMap);
    }
}
